package com.yqRecite.controller;

import cn.hutool.extra.tokenizer.Word;
import com.yqRecite.entity.*;
import com.yqRecite.service.*;
import com.yqRecite.util.result.Result;
import com.yqRecite.util.result.ResultUtil;
import com.yqRecite.util.util.ImgVideoUploadUtil;
import com.yqRecite.vo.AllPeople;
import com.yqRecite.vo.DynamicVo;
import com.yqRecite.vo.UserVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private AdminService adminService;
    @Resource
    private ImagesService imagesService;
    @Resource
    private WordsService wordsService;
    @Resource
    private DynamicService dynamicService;


    /**
     * 上传背景图片
     * @param file
     * @param request
     * @return
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping("uploadBgImg")
    public Result updateAvatar(@RequestParam("file") MultipartFile file,@RequestParam Map<String, String> params , HttpServletRequest request) throws IOException, ParseException {
        ImgVideoUploadUtil imgVideoUploadUtil = new ImgVideoUploadUtil();
        System.out.println("file==>"+file);
        String img = imgVideoUploadUtil.upload(file, request);
        Images images = new Images();
        if (img != null && !img.equals("")){
            images.setUid(Integer.parseInt(params.get("id")));
            images.setUrl(img);
            images.setCreateTime(new Date());
            return imagesService.addOneImage(images);
        }else{
            return ResultUtil.error("图片上传出现错误！", images);
        }
    }


    /**
     * 增加一个单词（收藏，加入生词本，喜欢等等）
     * @param words
     * @return
     */
    @RequestMapping("addOneWord")
    public Result addOneWord(@RequestBody Words words){
        return wordsService.addOneWord(words);
    }

    /**
     * 查询所有单词
     */
    @RequestMapping("searchAllWords")
    public Result searchAllWords(){
        return wordsService.getAllWordsList();
    }

    /**
     * 查询执行类型的单词
     * @param type
     */
    @RequestMapping("searchAllWordsByType/{type}")
    public Result searchAllWordsByType(@PathVariable("type") int type){
        return wordsService.getAllWordsListByType(type);
    }

    /**
     * 查询某个用户的指定类型的单词
     * @param uid 用户id
     * @param type 类型
     * @return
     */
    @RequestMapping("searchMyWordsByType/{uid}/{type}")
    public Result searchMyWordsByType(@PathVariable("uid") int uid, @PathVariable("type") int type){
        return wordsService.getMyWordsListByType(uid, type);
    }

    @RequestMapping("deleteWord/{id}")
    public Result deleteWord(@PathVariable("id") int id){
//        return ResultUtil.ok();//测试
        return wordsService.deleteWords(id);
    }

    /**
     * 上传一个动态
     * @param dynamicVo
     * @return
     */
    @RequestMapping("addOneDynamic")
    public Result addOneDynamic(@RequestBody DynamicVo dynamicVo){
        System.out.println("dynamic==>"+ dynamicVo);
        Dynamic dynamic = new Dynamic();
        dynamic.setUid(dynamicVo.getUid());
        dynamic.setUsername(dynamicVo.getUsername());
        dynamic.setAvatar(dynamicVo.getAvatar());
        dynamic.setContent(dynamicVo.getContent());
        dynamic.setImgUrls(String.join(",", dynamicVo.getImgUrls()));
        dynamic.setLikeList("");
        dynamic.setThumbList("");
        dynamic.setLikeNum(0);
        dynamic.setThumbNum(0);
        dynamic.setCreateTime(new Date());
        return dynamicService.addOneDynamic(dynamic);
    }


    /**
     * 获取所有动态
     * @return
     */
    @RequestMapping("getAllDynamics")
    public Result getAllDynamics(){
        return dynamicService.getAllDynamicList();
    }

    /**
     * 查询我上传过的动态
     * @param uid 用户id
     * @return
     */
    @RequestMapping("getAllMyDynamics/{uid}")
    public Result getAllMyDynamics(@PathVariable("uid") int uid){
        return dynamicService.getMyDynamicList(uid);
    }

    /**
     * 用户喜欢了动态
     * @param id
     * @param uid
     * @return
     */
    @RequestMapping("likeDynamic/{id}/{uid}")
    public Result likeDynamic(@PathVariable("id") int id, @PathVariable("uid") String uid){
        return dynamicService.likeOneDynamic(uid, id);
    }

    /**
     * 用户取消喜欢了动态
     * @param id
     * @param uid
     * @return
     */
    @RequestMapping("CancelLikeDynamic/{id}/{uid}")
    public Result CancelLikeDynamic(@PathVariable("id") int id, @PathVariable("uid") String uid){
        return dynamicService.cancelLikeOneDynamic(uid, id);
    }

    /**
     * 用户点赞了动态
     * @param id
     * @param uid
     * @return
     */
    @RequestMapping("thumbDynamic/{id}/{uid}")
    public Result thumbDynamic(@PathVariable("id") int id, @PathVariable("uid") String uid){
        return dynamicService.thumbOneDynamic(uid, id);
    }

    /**
     * 用户取消点赞了动态
     * @param id
     * @param uid
     * @return
     */
    @RequestMapping("CancelThumbDynamic/{id}/{uid}")
    public Result CancelThumbDynamic(@PathVariable("id") int id, @PathVariable("uid") String uid){
        return dynamicService.cancelThumbOneDynamic(uid, id);
    }

    /**
     * 进入动态内部
     * @param id
     * @return 动态详情，评论区列表
     */
    @RequestMapping("getDynamicInner/{id}")
    public Result getDynamicInner(@PathVariable("id") int id){
        return dynamicService.getDynamicInner(id);
    }

    /**
     * 删除动态
     * @param id
     * @return
     */
    @RequestMapping("delDynamic/{id}")
    public Result delDynamic(@PathVariable("id") int id){
        return dynamicService.deleteDynamic(id);
    }


    /**
     * 获取我的收藏，我的喜欢，我的生词本
     * @param uid
     * @return
     */
    @RequestMapping("getCollectNewNoteLove/{uid}")
    public Result getCollectNewNoteLove(@PathVariable("uid") int uid){
        System.out.println("uid===>"+uid);
        return wordsService.getCollectNewNoteLove(uid);
    }


    /**
     * 操作单词，加入和取消（收藏，生词本，喜欢）
     * @param uid 用户id
     * @param type 类型（1、收藏，2、生词本，3、喜欢）
     * @param words 单词数据
     * @return
     */
    @RequestMapping("operatorWord/{uid}/{type}")
    public Result operatorWord(@PathVariable("uid") int uid, @PathVariable("type") int type, @RequestBody Words words){
        System.out.println("words==>"+words);
        return wordsService.operatorWord(uid, type, words);
    }
}
